import bluebird from 'bluebird';
import * as core from './core';

module.exports = router => {
    // 解析问题
    function parseQuestion(reqBody) {
        const question = reqBody;
        const options = reqBody.options;
        delete question.options;
        return {question, options};
    }
    router.post('/', ctx => {
        return bluebird.method(() => {
            const {question, options} = parseQuestion(ctx.request.body);
            return core.add(question, options);
        })()
        .then(ctx.done)
        .catch(ctx.fail);
    });
    router.put('/:id', ctx => {
        return bluebird.method(() => {
            const {question, options} = parseQuestion(ctx.request.body);
            return core.edit(ctx.params.id, question, options);
        })()
        .then(ctx.done)
        .catch(ctx.fail);
    });
    router.delete('/:id', ctx => {
        return core.del(ctx.params.id)
            .then(ctx.done)
            .catch(ctx.fail);
    });
};
